<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @exception 400 参数错误
 * @exception 502 服务器错误
 * @exception 503 数据库错误
 */
class MainController extends CI_Controller
{
    use noAuth;


    /**
     * 登录认证
     * @desc 用户登录认证
     * @throws MyException
     */
    function addToken()
    {
        $data = $this->input->post(['username','password']);
        if (!$data) throw new MyException('', MyException::INPUT_MISS);
        restful(200, $this->account->login($data['username'], $data['password']));
    }


    /**
     * 跳转统一认证页面
     * @desc 跳转统一认证页面
     * @throws MyException
     */
    function oauth()
    {
        $url = $this->input->get('url');
        if (!$url) throw new MyException('', MyException::INPUT_MISS);
        $this->load->library('oauth');
        $url = $this->oauth->authUrl($url);
        jump($url);
    }


    /**
     * 统一认证登录回调
     * @desc 统一认证登录回调
     * @throws MyException
     */
    function oauthcallback()
    {
        $code = $this->input->get('code');
        if (!$code) throw new MyException('', MyException::INPUT_MISS);
        $this->load->library('oauth');
        $info = $this->oauth->userInfo($code);
        if (isset($info['openid'])) {
            restful(200, $this->account->oauth($info['openid']));
        } else {
            throw new MyException('认证失败,请重试', MyException::THIRD);
        }
    }
}